package com.jrx.anytxn.transaction.job.coupon.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.customer.dto.coupon.CouponReq;
import com.jrx.anytxn.customer.entity.TlRightsExchangeLog;
import com.jrx.anytxn.customer.service.ICouponInputService;
import com.jrx.anytxn.param.dto.rights.CardCouponDetailRes;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName CouponOrderStateUpdateProcessor
 *
 * @author lujianyou
 * @version 1.0
 * @description 权益订单状态更新 批处理Processor
 * @date 2021/11/29 16:40
 **/
@Service
public class CouponOrderStateUpdateProcessor implements ItemProcessor<TlRightsExchangeLog, TlRightsExchangeLog> {

    private static final Logger logger = LoggerFactory.getLogger(CouponOrderStateUpdateProcessor.class);

    @Autowired
    private ICouponInputService couponInputService;

    @Override
    public TlRightsExchangeLog process(TlRightsExchangeLog item) throws Exception {
        try {
            CouponReq couponReq = new CouponReq();
            couponReq.setSceneOrderId(item.getSceneOrderId());
            couponReq.setPlatOrderId(item.getCouponSystemOrderId());
            couponReq.setSeqNo(DateUtils.format(new Date(), "yyyyMMddHHmmssSSS") + RandomUtils.nextInt(100, 1000));
            CardCouponDetailRes cardCouponDetailRes = couponInputService.cardCouponDetail(couponReq);

            if (StringUtils.isEmpty(cardCouponDetailRes.getOrderState())) {
                return null;
            }

            if (!StringUtils.isEmpty(cardCouponDetailRes.getCardNo())) {
                item.setCardNo(cardCouponDetailRes.getCardNo());
            }

            if (!StringUtils.isEmpty(cardCouponDetailRes.getCardPassword())) {
                item.setCardPassword(cardCouponDetailRes.getCardPassword());
            }

            item.setOrderState(cardCouponDetailRes.getOrderState());

            return item;
        } catch (Exception e) {
            logger.error("权益状态查询失败，客户编号：{}，提示：{}", item.getCustomerId(), e.getMessage());
            return null;
        }
    }

}
